package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbCompanyBlacklist;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbCompanyCertBo;
import org.dromara.system.domain.vo.WbCompanyCertVo;
import org.dromara.system.domain.WbCompanyCert;
import org.dromara.system.mapper.WbCompanyCertMapper;
import org.dromara.system.service.IWbCompanyCertService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 企业资质证书Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WbCompanyCertServiceImpl implements IWbCompanyCertService {

    private final WbCompanyCertMapper baseMapper;

    /**
     * 查询企业资质证书
     */
    @Override
    public WbCompanyCertVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询企业资质证书列表
     */
    @Override
    public TableDataInfo<WbCompanyCertVo> queryPageList(WbCompanyCertBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbCompanyCert> lqw = buildQueryWrapper(bo);
//        lqw.groupBy(WbCompanyCert::getCertificateId);
        lqw.orderByDesc(WbCompanyCert::getId);
        Page<WbCompanyCertVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询企业资质证书列表
     */
    @Override
    public List<WbCompanyCertVo> queryList(WbCompanyCertBo bo) {
        LambdaQueryWrapper<WbCompanyCert> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbCompanyCert> buildQueryWrapper(WbCompanyCertBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbCompanyCert> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, WbCompanyCert::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), WbCompanyCert::getCompanyName, bo.getCompanyName());
        lqw.eq(bo.getCertificateId() != null, WbCompanyCert::getCertificateId, bo.getCertificateId());
        lqw.like(StringUtils.isNotBlank(bo.getCertificateName()), WbCompanyCert::getCertificateName, bo.getCertificateName());
        lqw.like(StringUtils.isNotBlank(bo.getCoverage()), WbCompanyCert::getCoverage, bo.getCoverage());
        lqw.eq(bo.getCertifyingTime() != null, WbCompanyCert::getCertifyingTime, bo.getCertifyingTime());
        lqw.eq(bo.getDataTagging() != null, WbCompanyCert::getDataTagging, bo.getDataTagging());
        lqw.eq(bo.getCertifyingEndTime() != null, WbCompanyCert::getCertifyingEndTime, bo.getCertifyingEndTime());
        lqw.like(StringUtils.isNotBlank(bo.getCertifyingAgencies()), WbCompanyCert::getCertifyingAgencies, bo.getCertifyingAgencies());
        return lqw;
    }

    /**
     * 新增企业资质证书
     */
    @Override
    public Boolean insertByBo(WbCompanyCertBo bo) {
        WbCompanyCert add = MapstructUtils.convert(bo, WbCompanyCert.class);
        validEntityBeforeSave(add);
        Long companyId = add.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        add.setCompanyName(companyName);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改企业资质证书
     */
    @Override
    public Boolean updateByBo(WbCompanyCertBo bo) {
        WbCompanyCert update = MapstructUtils.convert(bo, WbCompanyCert.class);
        validEntityBeforeSave(update);
        Long companyId = update.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        update.setCompanyName(companyName);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbCompanyCert entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除企业资质证书
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
